public class MementoPattern {
    public static void main(String[] args) {
        People people=new People();
        people.show();
        MementoCaretaker mementoCaretaker=new MementoCaretaker();
        mementoCaretaker.setMemento(people.save());
        people.stayUpLate();
        people.show();
        people.restore(mementoCaretaker.memento);
        people.show();
    }
}

class People {
    int healthLevel;

    public People() {
        healthLevel = 10;
    }

    public void stayUpLate() {
        healthLevel = 1;
    }

    public Memento save() {
        return new Memento(healthLevel);
    }
    public void restore(Memento memento){
        this.healthLevel=memento.healthLevel;
    }
    public void show(){
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "People [healthLevel=" + healthLevel + "]";
    }
    
}
/* 
 * 将对象状态存储在外部
 * 状态的保存恢复逻辑和客户端分离
 * 想要更改状态的保存恢复逻辑不需要去修改客户端
 */
class Memento {
    int healthLevel;

    public Memento(int healthLevel) {
        this.healthLevel = healthLevel;
    }

}

class MementoCaretaker {
    Memento memento;
    public void setMemento(Memento memento) {
        this.memento = memento;
    }

}